package com.xuecheng.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.basic.domain.Category;
import com.xuecheng.basic.mappers.CategoryMapper;
import com.xuecheng.basic.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.commons.model.vo.CategoryVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程分类 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-09-19
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    //查询课程分类信息
    @Override
    public ResponseResult<CategoryVo> findTreeNodes() {
        //1.查询1级课程分类：parentId=0
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getParentid,"0");
        Category category = this.getOne(wrapper);
        CategoryVo one = BeanHelper.copyProperties(category, CategoryVo.class);
        //2.查询2级课程分类：parentId=1
        List<CategoryVo> vos = this.findByParentId("1");
        one.setCategoryTreeNodes(vos);
        //3.查询3级课程分类：parentId
        for (CategoryVo two : vos) {
            List<CategoryVo> three = this.findByParentId(two.getId());
            two.setCategoryTreeNodes(three);
        }
        return ResponseResult.okResult(one);
    }
    //根据parentId查询分类信息并转成List<CategoryVo>
    public List<CategoryVo> findByParentId(String parentId) {
        LambdaQueryWrapper<Category> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Category::getParentid,parentId);
        List<Category> list = this.list(wrapper1);
        return BeanHelper.copyWithCollection(list, CategoryVo.class);
    }
}
